import {Component, OnInit, Output} from '@angular/core';
import {PageHelper} from '../../../../utils/page-helper-model';
import {BaseService} from '../../../../shared/service/base.service';
import {ApiUrl} from '../../../../config/api-url';
import {Globals} from '../../../../globals';

@Component({
  selector: 'app-scheduler-list',
  templateUrl: './scheduler-list.component.html',
  styleUrls: ['./scheduler-list.component.less']
})
export class SchedulerListComponent implements OnInit {
  // 查看运行当前设备
  isMobile = Globals.isMobile();
  // 数据集合
  listOfData = [];
  // 显示模态框
  isVisible: boolean;
  // 分页
  pageHelper: PageHelper = new PageHelper();
  // 加载动画
  isOkLoading: boolean;
  // 当前任务对象，用来修改
  currentJob: any;

  constructor(private baseService: BaseService) {
  }

  ngOnInit() {
    this.findList();
  }

  // 暂停任务
  pauseJob(job: any) {
    this.baseService.layer.modalConfirm('确定要暂停任务吗？', result => {
      if (result) {
        let body = {
          jobName: job.job_NAME,
          jobGroup: job.job_GROUP
        };
        this.baseService.put(ApiUrl.JobUrl + '/pause', body).subscribe(
          data => {
            if (data.status == 0) {
              this.baseService.layer.messageSuccess('修改任务成功！');
              this.loadData();
            } else {
              this.baseService.layer.messageError('修改失败：' + data.msg);
            }
          }
        );
      }
    });
  }

  // 恢复任务
  recoverJob(job: any) {
    console.log(job);
    this.baseService.layer.modalConfirm('确定要恢复任务吗？', result => {
      if (result) {
        let body = {
          jobName: job.job_NAME,
          jobGroup: job.job_GROUP
        };
        this.baseService.put(ApiUrl.JobUrl + '/resume', body).subscribe(
          data => {
            if (data.status == 0) {
              this.baseService.layer.messageSuccess('恢复任务成功！');
              this.loadData();
            } else {
              this.baseService.layer.messageError('恢复失败：' + data.msg);
            }
          }
        );
      }
    });
  }

  // 删除任务
  deleteJob(job: any) {
    this.baseService.layer.modalConfirm('确定要删除任务吗？', result => {
      if (result) {
        let body = {
          jobName: job.job_NAME,
          jobGroup: job.job_GROUP
        };
        this.baseService.put(ApiUrl.JobUrl + '/delete', body).subscribe(
          data => {
            if (data.status == 0) {
              this.baseService.layer.messageSuccess('删除任务成功！');
              this.loadData();
            } else {
              this.baseService.layer.messageError('删除失败：' + data.msg);
            }
          }
        );
      }
    });
  }

  // 修改任务
  updateJob(data: any) {
    this.currentJob = data;
    this.isVisible = true;
  }

  // 添加任务
  addJob() {
    this.currentJob = null;
    this.isVisible = true;
  }

  // 获取分页列表
  findList() {
    this.baseService.findListByKeyword(ApiUrl.JobUrl + '/list', this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          console.log(data.data.list)
          this.listOfData = data.data.list;
          this.pageHelper.totalItems = data.data.total;
          this.pageHelper.numPages = data.data.pages;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  // 刷新
  refreshJob() {
    this.loadData();
  }

  // 上下页
  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findList();
  }

  // 刷新数据
  loadData() {
    this.pageHelper.currentPage = 1;
    this.findList();
  }

  // 子组件向父组件传值
  setVisible(body) {
    this.isVisible = body.isVisible;

    if (body.addOrUpdate){
      this.loadData();
    }
  }
}
